package member;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import model.*;

import util.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class SigninAction extends DispatchAction {

	
	public ActionForward signinaction(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MemberForm mf = (MemberForm)form;
		MemberDAO memberDAO = new MemberDAO();
		
		//MemberForm to MemberDTO
		Member member = new Member();
		member.setMid(mf.getMid().toLowerCase());
		member.setMpass(mf.getMpass());
		member.setMname(mf.getMname());
		member.setLvno(1);
		member.setMemail(mf.getMemail());
		member.setMbirth(mf.getMbirth());
		member.setMaddressh(mf.getMaddressh());
		member.setMaddresso(mf.getMaddresso());
		member.setMphoneh(mf.getMphoneh());
		member.setMphoneo(mf.getMphoneo());
		member.setMintro(mf.getMintro());
		
		member.setMoemail(mf.getMoemail());
		member.setMobirth(mf.getMobirth());
		member.setMoaddressh(mf.getMoaddressh());
		member.setMoaddresso(mf.getMoaddresso());
		member.setMophoneh(mf.getMophoneh());
		member.setMophoneo(mf.getMophoneo());
		member.setMointro(mf.getMointro());
		member.setMophoto(mf.getMophoto());
		member.setMomailing(mf.getMomailing());
		
		System.out.println(mf.getMoemail());
		System.out.println(mf.getMobirth());
		
		if(mf.getMphoto().getFileSize()>0){		
			
			System.out.println("Mphoto : "+mf.getMphoto());
			System.out.println("Mfilesize : "+mf.getMphoto().getFileSize());
			
			ServletContext sc = request.getSession().getServletContext();		
			String originalFileName = mf.getMphoto().getFileName();		
			
			
			String fileName = sc.getRealPath("/uploadfiles/"+originalFileName);
			
			InputStream is = mf.getMphoto().getInputStream();
			String sfn = FileSaver.save(fileName, is);	
			
			String savedFileName = sfn.substring(sfn.lastIndexOf("\\")+1);
			
			member.setMphotoofn(originalFileName);
			member.setMphotosfn(savedFileName);
		}
		
		//
		
		int result = memberDAO.insert(member);
				
		ActionForward af = new ActionForward();
		
		switch(result){
		case 0:						
			af.setPath("signin.do");			
			break;
		case 1:
			HttpSession session = request.getSession();
			User user = new User();
			user.setLogon(true);
			user.setUserInfo(memberDAO.getMemberByMid(mf.getMid()));
			user.setMid(member.getMid());
			session.setAttribute("user", user);
			af.setPath("signinsubmit.do");
			
			break;
		}
		
		af.setRedirect(true);
		
		return af;
		
	}

	public ActionForward validateMid(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		String result = "";
		
		System.out.println("요청 수신");
		
		String mid = request.getParameter("mid");
		MemberDAO memberDAO = new MemberDAO();
		
		int r = memberDAO.getMemberByLetter(mid);
		if(r!=0){
			result = "invalid";
		} else result = "ok";
		System.out.println(result);
		PrintWriter out = response.getWriter();
		out.print(result);
		out.close();
		return null;
	}

	public ActionForward signout(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		MemberForm mf = (MemberForm)form;
		User user = (User)request.getSession().getAttribute("user");
		String mid = user.getMid();
		
		MemberDAO memberDAO = new MemberDAO();
		String mpass = mf.getMpass();
		
		ActionForward af = new ActionForward();
		
		Member member = memberDAO.getMemberByMid(mid);
		if(member.getMpass().equals(mpass))	{
			if(memberDAO.delete(mid)==1){
				System.out.println("탈퇴성공");				
				request.getSession().setAttribute("user", null);
				return mapping.findForward("success");
			}
		} 
		
		System.out.println("탈퇴실패");
		af.setModule("/member");
		af.setPath("signout.do");	
		af.setRedirect(true);
		
		return af;
	}
	
	
	
}
